import os
import sys
os.environ["CUDA_VISIBLE_DEVICES"]="0"
sys.path.insert(1,'/home/jgozlan/GIT/scripts/')
sys.path.insert(1,'/home/jgozlan/GIT/models/')
sys.path.insert(1, '/home/jgozlan/GIT/models_analysis/')
#from tensorflow import keras
import keras
from keras import losses
from keras.callbacks import EarlyStopping
from keras.optimizers import adam
from keras.models import Model, Sequential
from keras import backend as K
from keras.metrics import MeanAbsolutePercentageError
import matplotlib.pyplot as plt
import numpy as np
import samplerate
from numpy import array, hstack, unique, where
import statsmodels.api as sm
import tensorflow as tf
tf.test.is_gpu_available()
from imu_extractor import get_all_dataframes_for_forecasting, get_freq_magn, transform_dataset_into_freq_magn
from train_test_split import get_train_test_split
from baseline import find_frequency,repeat_period_forecast_acf, get_mean_line_forecast, get_flat_line_forecast
from metrics import compare_method, plot_few_prediction
from models import create_lstm_encoder_decoder
path_cluster0 = ['Run_Features','Walk_Features']
path_cluster1 = ['CarDriving_Features']
path_cluster2 = ['Karting_Features',
'MotorcycleHelmet_Features',
'Scooter_Features','SkateboardChesty_Features',
'SnowboardSeeker_Features','Hiking_Features']
path_all_classes = path_cluster0 + path_cluster1 + path_cluster2
recording_freq = 6400.0
downsampling_freq = 200.0
all_dfs_classes = []
for i,c in enumerate(path_all_classes):
dfs = get_all_dataframes_for_forecasting("Database/" + c, recording_freq, downsampling_freq)
all_dfs_classes.append(dfs)
all_df = [item for sublist in all_dfs_classes for item in sublist]
lag = 500
ahead = 100
delay = 5
test_size = 0.2
dim = 12
target_index = [0,1,2]
classification = False
X_train, y_train, X_test, y_test = get_train_test_split(all_df, test_size, lag, ahead, delay, target_index, classification = False)
num_freq = 12
transformed_X_train = transform_dataset_into_freq_magn(X_train[:,:,:6], downsampling_freq, num_freq)
transformed_X_test = transform_dataset_into_freq_magn(X_test[:,:,:6], downsampling_freq, num_freq)
freq_dim_in = transformed_X_train.shape[1]
transformed_X_test.shape
def create_dropout_predict_function(model, dropout):
"""
Create a keras function to predict with dropout
model : keras model
dropout : fraction dropout to apply to all layers
Returns
predict_with_dropout : keras function for predicting with dropout
"""
# Load the config of the original model
conf = model.get_config()
# Add the specified dropout to all layers
for layer in conf['layers']:
# Dropout layers
if layer["class_name"]=="Dropout":
#print("1")
layer["config"]["rate"] = dropout
# Recurrent layers with dropout
elif "dropout" in layer["config"].keys():
#print("2")
#print(layer)
#print(layer["config"]["dropout"])
layer["config"]["dropout"] = dropout
# Create a new model with specified dropout
if type(model)==Sequential:
# Sequential
model_dropout = Sequential.from_config(conf)
else:
# Functional
model_dropout = Model.from_config(conf)
model_dropout.set_weights(model.get_weights())
# Create a function to predict with the dropout on
predict_with_dropout = K.function(model_dropout.inputs+[K.learning_phase()], model_dropout.outputs)
return predict_with_dropout
def get_stats_from_pred(pred_with_dropout, input_pred, num_iter, ci = 0.8):
predictions = np.zeros((num_iter, ahead))
for i in range(num_iter):
pred = pred_with_dropout((input_pred ,1.0))
predictions[i,:] = pred[0].reshape((1,50))
means = predictions.mean(axis=0)
sds = predictions.std(axis = 0)
lows = np.quantile(predictions, 0.5-ci/2, axis=0)
uppers = np.quantile(predictions, 0.5+ci/2, axis=0)
return means, sds, lows, uppers
def get_mean_std_ci_from_pred(pred_with_dropout, input_pred, num_iter = 30, ci = 0.8):
predictions = np.zeros((num_iter, ahead,3))
for i in range(num_iter):
pred = pred_with_dropout((input_pred ,1.0))
predictions[i,:] = pred[0].reshape((1,ahead,3))
means_x = predictions[:,:,0].reshape((-1,100)).mean(axis=0)
means_y = predictions[:,:,1].reshape((-1,100)).mean(axis=0)
means_z = predictions[:,:,2].reshape((-1,100)).mean(axis=0)
sds_x = predictions[:,:,0].reshape((-1,100)).std(axis=0)
sds_y = predictions[:,:,1].reshape((-1,100)).std(axis=0)
sds_z = predictions[:,:,2].reshape((-1,100)).std(axis=0)
lows_x = np.quantile(predictions[:,:,0], 0.5- ci /2, axis=0)
uppers_x = np.quantile(predictions[:,:,0], 0.5+ ci /2, axis=0)
lows_y = np.quantile(predictions[:,:,1], 0.5- ci /2, axis=0)
uppers_y = np.quantile(predictions[:,:,1], 0.5+ ci /2, axis=0)
lows_z = np.quantile(predictions[:,:,2], 0.5- ci /2, axis=0)
uppers_z = np.quantile(predictions[:,:,2], 0.5+ ci /2, axis=0)
means = [means_x, means_y, means_z]
sds = [sds_x, sds_y, sds_z]
lows = [lows_x, lows_y, lows_z]
uppers = [uppers_x, uppers_y, uppers_z]
return means, sds, lows, uppers
# MODELS IMPORTcluster4_classification_model_new
cluster_classification_model = keras.models.load_model('cluster4_classification_model_new')
#global_forecasting_model = keras.models.load_model('models_analysis/global_forecasting_multi_5step',custom_objects={"MeanAbsolutePercentageError":MeanAbsolutePercentageError()}, compile= False)
cluster0_model = keras.models.load_model('cluster0_forecasting_multi_5step', custom_objects={"MeanAbsolutePercentageError":MeanAbsolutePercentageError()}, compile= False)
cluster1_model = keras.models.load_model('cluster1_forecasting_multi_5step', custom_objects={"MeanAbsolutePercentageError":MeanAbsolutePercentageError()}, compile= False)
cluster2_model = keras.models.load_model('cluster2_forecasting_multi_5step', custom_objects={"MeanAbsolutePercentageError":MeanAbsolutePercentageError()}, compile= False)
cluster3_model = keras.models.load_model('cluster3_forecasting_multi_5step', custom_objects={"MeanAbsolutePercentageError":MeanAbsolutePercentageError()}, compile= False)
dropout = 0.05
cluster0_predict_with_dropout = create_dropout_predict_function(cluster0_model, dropout)
cluster1_predict_with_dropout = create_dropout_predict_function(cluster1_model, dropout)
cluster2_predict_with_dropout = create_dropout_predict_function(cluster2_model, dropout)
cluster3_predict_with_dropout = create_dropout_predict_function(cluster3_model, dropout)
cluster_models = [cluster0_model, cluster1_model, cluster2_model, cluster3_model]
cluster_dropout_models = [cluster0_predict_with_dropout, cluster1_predict_with_dropout, cluster2_predict_with_dropout, cluster3_predict_with_dropout]
global_forecast = global_forecasting_model.predict(X_test)
def get_acf_for_signal(input_, lag):
acf = sm.tsa.acf(input_, nlags = lag)
inflection = np.diff(np.sign(np.diff(acf)))
peaks = (inflection < 0).nonzero()[0] + 1
acf_peaks = np.argsort(-1*acf[peaks])
acf_peaks_sorted = acf[peaks][acf_peaks]
if len(acf_peaks_sorted) > 0:
return acf_peaks_sorted[0]
else:
return 0
def get_acf_value(samples, lag, delay):
reconstitued_signal_x = np.zeros((lag + (len(samples)-1)*delay))
reconstitued_signal_y = np.zeros((lag + (len(samples)-1)*delay))
reconstitued_signal_z = np.zeros((lag + (len(samples)-1)*delay))
for i,sample in enumerate(samples):
if i != len(samples)-1:
reconstitued_signal_x[(i*delay): (i*delay) + delay] = sample[:,0][:5]
reconstitued_signal_y[(i*delay): (i*delay) + delay] = sample[:,1][:5]
reconstitued_signal_z[(i*delay): (i*delay) + delay] = sample[:,2][:5]
else:
reconstitued_signal_x[(i*delay):] = sample[:,0]
reconstitued_signal_y[(i*delay):] = sample[:,1]
reconstitued_signal_z[(i*delay):] = sample[:,2]
#print(reconstitued_signal_x.shape)
x_peak = get_acf_for_signal(reconstitued_signal_x, lag)
y_peak = get_acf_for_signal(reconstitued_signal_y, lag)
z_peak = get_acf_for_signal(reconstitued_signal_z, lag)
return np.mean([x_peak,y_peak, z_peak])
def repeat_period_forecast_acf_new(input_, lag, ahead, i):
acf = sm.tsa.acf(input_, nlags = lag)
inflection = np.diff(np.sign(np.diff(acf)))
peaks = (inflection < 0).nonzero()[0] + 1
acf_top_peak = np.argsort(-1*acf[peaks])
if len(acf_top_peak) < 1:
return np.ones(ahead) * input_[-1:]
top_peak_index = peaks[acf_top_peak][0]
last_period_value = input_[-top_peak_index:]
if ahead <= top_peak_index:
#print(last_period_value[:ahead])
return last_period_value[:ahead]
else:
nb_period = int(ahead/top_peak_index)
repeated_period = np.array(last_period_value, copy=True)
for i in range(nb_period -1):
repeated_period = np.concatenate((repeated_period, last_period_value), axis = None)
extra = ahead % top_peak_index
if extra:
repeated_period = np.concatenate((repeated_period, last_period_value[:extra]), axis = None)
#print(repeated_period)
return repeated_period
predictions_cluster0 = cluster0_model.predict(X_test)
predictions_cluster1 = cluster1_model.predict(X_test)
predictions_cluster2 = cluster2_model.predict(X_test)
predictions_cluster3 = cluster3_model.predict(X_test)
predictions_clusters = [predictions_cluster0, predictions_cluster1, predictions_cluster2, predictions_cluster3]
batch_pipeline_size = 100
threshold_softmax = 0.75
threshold_acf = 0.6
predictions_cluster_class_acf = np.zeros((X_test.shape[0], ahead, 3))
predictions_cluster_class_acf_index = np.ones((X_test.shape[0])) * -1
predictions_cluster_class = np.zeros((X_test.shape[0], ahead, 3))
for i in range(0, len(X_test), batch_pipeline_size):
if (i % 500) == 0:
print(i)
batch_time = X_test[i: (i+ batch_pipeline_size)]
batch_freq = transformed_X_test[i: (i+ batch_pipeline_size)]
cluster_probs = cluster_classification_model.predict(batch_freq)
cluster_probs_filtered = cluster_probs > threshold_softmax
idx0 = np.where(cluster_probs_filtered, cluster_probs ,np.nanmin(cluster_probs)-threshold_softmax).argmax(1)
idx = np.where(cluster_probs_filtered.any(1), idx0, -1)
if all(x == idx[0] for x in idx) and idx[0] != -1:
acf_value = get_acf_value(batch_time[:,:,:3], lag, delay)
predictions_cluster_class[i:i+batch_pipeline_size] = predictions_clusters[idx[0]][i: i + batch_pipeline_size]
if acf_value > threshold_acf:
predictions_cluster_class_acf[i: i + batch_pipeline_size] = predictions_clusters[idx[0]][i: i + batch_pipeline_size]
predictions_cluster_class_acf_index[i: i + batch_pipeline_size] = idx[0]
idx_threshold = np.where(predictions_cluster_class_acf_index != -1)[0]
ratio = len(idx_threshold)/X_test.shape[0]
print(ratio)
import numpy as np
forecast = predictions_cluster_class
forecast_x = forecast[:,:,0].reshape((forecast.shape[0], forecast.shape[1]))
forecast_y = forecast[:,:,1].reshape((forecast.shape[0], forecast.shape[1]))
forecast_z = forecast[:,:,2].reshape((forecast.shape[0], forecast.shape[1]))
forecast_acf = predictions_cluster_class_acf
forecast_x_acf = forecast_acf[:,:,0].reshape((forecast_acf.shape[0], forecast_acf.shape[1]))
forecast_y_acf = forecast_acf[:,:,1].reshape((forecast_acf.shape[0], forecast_acf.shape[1]))
forecast_z_acf = forecast_acf[:,:,2].reshape((forecast_acf.shape[0], forecast_acf.shape[1]))
forecast_x_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 0], lag, ahead, i) for i in range(X_test.shape[0])]
forecast_x_period_acf = np.array(forecast_x_period_acf)
forecast_y_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 1], lag, ahead, i) for i in range(X_test.shape[0])]
forecast_y_period_acf = np.array(forecast_y_period_acf)
forecast_z_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 2], lag, ahead, i) for i in range(X_test.shape[0])]
forecast_z_period_acf = np.array(forecast_z_period_acf)
true_x = y_test[:,:,0].reshape((y_test.shape[0], y_test.shape[1]))
true_y = y_test[:,:,1].reshape((y_test.shape[0], y_test.shape[1]))
true_z = y_test[:,:,2].reshape((y_test.shape[0], y_test.shape[1]))
import random
from evaluation2 import plot_forecasts2, plot_metrics
import matplotlib.pyplot as plt
def plot_forecasts_test(X_true, y_true, forecasts, forecasts_color_dict, target_index, k):
random_indexes = random.sample(range(X_true.shape[0]), k)
for i in random_indexes:
plt.figure()
X_input = X_true[i][:,target_index].reshape((lag,-1))[-100:]
t = [len(X_input) + j for j in range(len(y_true[i]))]
plt.plot(range(len(X_input)), X_input, label = 'x_gyro input', color = "green")
plt.plot(t, y_true[i], label = 'x_gyro_true', color = "green")
for name_forecast, forecast in forecasts.items():
plt.plot(t, forecast[i], label = name_forecast, color = dict_forecasts_color[name_forecast])
plt.legend(loc="upper left")
plt.show()
dict_forecasts_x = {"forecast": forecast_x, "forecast_acf": forecast_x_acf, "period_acf": forecast_x_period_acf}
dict_forecasts_y = {"forecast": forecast_y,"forecast_acf": forecast_y_acf,"period_acf": forecast_y_period_acf}
dict_forecasts_z = {"forecast": forecast_z,"forecast_acf": forecast_z_acf, "period_acf": forecast_z_period_acf}
dict_forecasts_color = {"forecast": "blue", "forecast_acf": "orange", "period_acf": "brown","true": "green"}
plot_metrics(true_x, dict_forecasts_x, dict_forecasts_color)
plot_forecasts_test(X_test, true_x, dict_forecasts_x, dict_forecasts_color, 0, 20)
forecast_for_threshold_acf = predictions_cluster_class_acf[idx_threshold]
forecast_x_acf = forecast_for_threshold_acf[:,:,0].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_y_acf = forecast_for_threshold_acf[:,:,1].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_z_acf = forecast_for_threshold_acf[:,:,2].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_x_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 0], lag, ahead, i) for i in idx_threshold]
forecast_x_period_acf = np.array(forecast_x_period_acf)
forecast_y_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 1], lag, ahead, i) for i in idx_threshold]
forecast_y_period_acf = np.array(forecast_y_period_acf)
forecast_z_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 2], lag, ahead, i) for i in idx_threshold ]
forecast_z_period_acf = np.array(forecast_z_period_acf)
true_x = y_test[idx_threshold,:,0].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_y = y_test[idx_threshold,:,1].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_z = y_test[idx_threshold,:,2].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
dict_forecasts_x = {"forecast_acf": forecast_x_acf, "period_acf": forecast_x_period_acf}
dict_forecasts_y = {"forecast_acf": forecast_y_acf,"period_acf": forecast_y_period_acf}
dict_forecasts_z = {"forecast_acf": forecast_z_acf, "period_acf": forecast_z_period_acf}
dict_forecasts_color = {"forecast": "blue", "forecast_acf": "orange", "period_acf": "brown","true": "green"}
plot_metrics(true_x, dict_forecasts_x, dict_forecasts_color)
plot_forecasts_test(X_test[idx_threshold], true_x, dict_forecasts_x, dict_forecasts_color, 0, 20)
idx_threshold = np.where(predictions_cluster_class_acf_index == 0)[0]
forecast_for_threshold_acf = predictions_cluster_class_acf[idx_threshold]
forecast_x_acf = forecast_for_threshold_acf[:,:,0].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_y_acf = forecast_for_threshold_acf[:,:,1].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_z_acf = forecast_for_threshold_acf[:,:,2].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_x_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 0], lag, ahead, i) for i in idx_threshold]
forecast_x_period_acf = np.array(forecast_x_period_acf)
forecast_y_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 1], lag, ahead, i) for i in idx_threshold]
forecast_y_period_acf = np.array(forecast_y_period_acf)
forecast_z_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 2], lag, ahead, i) for i in idx_threshold ]
forecast_z_period_acf = np.array(forecast_z_period_acf)
true_x = y_test[idx_threshold,:,0].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_y = y_test[idx_threshold,:,1].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_z = y_test[idx_threshold,:,2].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
dict_forecasts_x = {"forecast_acf": forecast_x_acf, "period_acf": forecast_x_period_acf}
dict_forecasts_y = {"forecast_acf": forecast_y_acf,"period_acf": forecast_y_period_acf}
dict_forecasts_z = {"forecast_acf": forecast_z_acf, "period_acf": forecast_z_period_acf}
dict_forecasts_color = {"forecast": "blue", "forecast_acf": "orange", "period_acf": "brown","true": "green"}
plot_metrics(true_x, dict_forecasts_x, dict_forecasts_color)
plot_forecasts_test(X_test[idx_threshold], true_x, dict_forecasts_x, dict_forecasts_color, 0, 20)
idx_threshold = np.where(predictions_cluster_class_acf_index == 1)[0]
forecast_for_threshold_acf = predictions_cluster_class_acf[idx_threshold]
forecast_x_acf = forecast_for_threshold_acf[:,:,0].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_y_acf = forecast_for_threshold_acf[:,:,1].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_z_acf = forecast_for_threshold_acf[:,:,2].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_x_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 0], lag, ahead, i) for i in idx_threshold]
forecast_x_period_acf = np.array(forecast_x_period_acf)
forecast_y_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 1], lag, ahead, i) for i in idx_threshold]
forecast_y_period_acf = np.array(forecast_y_period_acf)
forecast_z_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 2], lag, ahead, i) for i in idx_threshold ]
forecast_z_period_acf = np.array(forecast_z_period_acf)
true_x = y_test[idx_threshold,:,0].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_y = y_test[idx_threshold,:,1].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_z = y_test[idx_threshold,:,2].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
dict_forecasts_x = {"forecast_acf": forecast_x_acf, "period_acf": forecast_x_period_acf}
dict_forecasts_y = {"forecast_acf": forecast_y_acf,"period_acf": forecast_y_period_acf}
dict_forecasts_z = {"forecast_acf": forecast_z_acf, "period_acf": forecast_z_period_acf}
dict_forecasts_color = {"forecast": "blue", "forecast_acf": "orange", "period_acf": "brown","true": "green"}
plot_metrics(true_x, dict_forecasts_x, dict_forecasts_color)
plot_forecasts_test(X_test[idx_threshold], true_x, dict_forecasts_x, dict_forecasts_color, 0, 20)
idx_threshold = np.where(predictions_cluster_class_acf_index == 2)[0]
forecast_for_threshold_acf = predictions_cluster_class_acf[idx_threshold]
forecast_x_acf = forecast_for_threshold_acf[:,:,0].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_y_acf = forecast_for_threshold_acf[:,:,1].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_z_acf = forecast_for_threshold_acf[:,:,2].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_x_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 0], lag, ahead, i) for i in idx_threshold]
forecast_x_period_acf = np.array(forecast_x_period_acf)
forecast_y_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 1], lag, ahead, i) for i in idx_threshold]
forecast_y_period_acf = np.array(forecast_y_period_acf)
forecast_z_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 2], lag, ahead, i) for i in idx_threshold ]
forecast_z_period_acf = np.array(forecast_z_period_acf)
true_x = y_test[idx_threshold,:,0].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_y = y_test[idx_threshold,:,1].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_z = y_test[idx_threshold,:,2].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
dict_forecasts_x = {"forecast_acf": forecast_x_acf, "period_acf": forecast_x_period_acf}
dict_forecasts_y = {"forecast_acf": forecast_y_acf,"period_acf": forecast_y_period_acf}
dict_forecasts_z = {"forecast_acf": forecast_z_acf, "period_acf": forecast_z_period_acf}
dict_forecasts_color = {"forecast": "blue", "forecast_acf": "orange", "period_acf": "brown","true": "green"}
plot_metrics(true_x, dict_forecasts_x, dict_forecasts_color)
plot_forecasts_test(X_test[idx_threshold], true_x, dict_forecasts_x, dict_forecasts_color, 0, 20)
idx_threshold = np.where(predictions_cluster_class_acf_index == 3)[0]
forecast_for_threshold_acf = predictions_cluster_class_acf[idx_threshold]
forecast_x_acf = forecast_for_threshold_acf[:,:,0].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_y_acf = forecast_for_threshold_acf[:,:,1].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_z_acf = forecast_for_threshold_acf[:,:,2].reshape((forecast_for_threshold_acf.shape[0], forecast_for_threshold_acf.shape[1]))
forecast_x_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 0], lag, ahead, i) for i in idx_threshold]
forecast_x_period_acf = np.array(forecast_x_period_acf)
forecast_y_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 1], lag, ahead, i) for i in idx_threshold]
forecast_y_period_acf = np.array(forecast_y_period_acf)
forecast_z_period_acf = [repeat_period_forecast_acf_new(X_test[i][:, 2], lag, ahead, i) for i in idx_threshold ]
forecast_z_period_acf = np.array(forecast_z_period_acf)
true_x = y_test[idx_threshold,:,0].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_y = y_test[idx_threshold,:,1].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
true_z = y_test[idx_threshold,:,2].reshape((y_test[idx_threshold].shape[0], y_test[idx_threshold].shape[1]))
dict_forecasts_x = {"forecast_acf": forecast_x_acf, "period_acf": forecast_x_period_acf}
dict_forecasts_y = {"forecast_acf": forecast_y_acf,"period_acf": forecast_y_period_acf}
dict_forecasts_z = {"forecast_acf": forecast_z_acf, "period_acf": forecast_z_period_acf}
dict_forecasts_color = {"forecast": "blue", "forecast_acf": "orange", "period_acf": "brown","true": "green"}
plot_metrics(true_x, dict_forecasts_x, dict_forecasts_color)
plot_forecasts_test(X_test[idx_threshold], true_x, dict_forecasts_x, dict_forecasts_color, 0, 20)
import numpy as np
cluster_forecasting_pred = np.zeros((X_test.shape[0], ahead, 1 ))
index_cluster0 = np.where(predicted_cluster_classes == 0)[0]
index_cluster1 = np.where(predicted_cluster_classes == 1)[0]
index_cluster2 = np.where(predicted_cluster_classes == 2)[0]
cluster_forecasting_pred[index_cluster0] = forecasting_cluster0_model.predict(X_test[index_cluster0])
cluster0_forecasting_pred[index_cluster1] =
#for i,pred in np.ndenumerate(predicted_cluster_classes):
#cluster_forecasting_pred[i] = global_forecasting_model.predict(X_test[i].reshape((1,lag, dim)))
global_forecasting_pred = global_forecasting_pred.reshape((global_forecasting_pred.shape[0],global_forecasting_pred.shape[1]))
cluster_forecasting_pred = cluster_forecasting_pred.reshape((cluster_forecasting_pred.shape[0],cluster_forecasting_pred.shape[1]))
cluster0_forecasting_pred = cluster0_forecasting_pred.reshape((cluster0_forecasting_pred.shape[0],cluster0_forecasting_pred.shape[1]))
cluster1_forecasting_pred = cluster1_forecasting_pred.reshape((cluster1_forecasting_pred.shape[0],cluster1_forecasting_pred.shape[1]))
cluster2_forecasting_pred = cluster2_forecasting_pred.reshape((cluster2_forecasting_pred.shape[0],cluster2_forecasting_pred.shape[1]))
forecast_flat = [get_flat_line_forecast(X_test[i][:, 0][-1], ahead) for i in range(X_test.shape[0])]
forecast_flat = np.array(forecast_flat)
forecast_period_acf = [repeat_period_forecast_acf(X_test[i][:, 0], lag, ahead, i) for i in range(X_test.shape[0])]
forecast_period_acf = np.array(forecast_period_acf)
true = y_test.reshape((-1,ahead))
dict_forecasts = {"forecast_global": global_forecasting_pred,
"forecast_cluster": cluster_forecasting_pred,
"forecast_cluster0": cluster0_forecasting_pred,
"forecast_cluster1": cluster1_forecasting_pred,
"forecast_cluster2": cluster2_forecasting_pred,
"flat": forecast_flat,
"period acf": forecast_period_acf}
dict_forecasts_color = {"forecast_global": "black",
"forecast_cluster": "blue",
"forecast_cluster0": "yellow",
"forecast_cluster1": "purple",
"forecast_cluster2": "orange",
"flat": "red",
"period acf": "brown"}
compare_method(true, dict_forecasts)
plot_few_prediction(X_test, true, dict_forecasts, 20)